﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Cs2102BookingSystem.Models;

namespace Cs2102BookingSystem.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
        BookingEntities _db = new BookingEntities();


        public ActionResult About()
        {
            return View();
        }
        public ActionResult Index()
        {

            ViewData["Message"] = "";

            var top10booking = (from booking in _db.BOOKING
                                group booking by new { booking.H_NAME, booking.H_COUNTRY, booking.H_BRANCHNAME } into hotelGrps
                                let sum = hotelGrps.Count()
                                orderby sum descending
                                select new
                                {
                                    Hotel = hotelGrps.Key.H_NAME,
                                    Branch = hotelGrps.Key.H_BRANCHNAME,
                                    Country = hotelGrps.Key.H_COUNTRY

                                });


            var top_10 = (from cheapestRoom in _db.LOWEST_HOTEL_PRICE
                          from top in top10booking
                          from hotel in _db.HOTEL

                          where top.Hotel == cheapestRoom.H_NAME &&
                                top.Hotel == hotel.HOTEL_NAME &&
                                top.Branch == cheapestRoom.H_BRANCHNAME &&
                                top.Branch == hotel.BRANCHNAME &&
                                top.Country == hotel.COUNTRY &&
                                top.Country == cheapestRoom.H_COUNTRY

                          select new
                          {
                              HOTELIMAGE = hotel.HOTELIMAGE,
                              HOTEL = hotel.HOTEL_NAME,
                              Branch = hotel.BRANCHNAME,
                              Country = hotel.COUNTRY,
                              Price = cheapestRoom.LOWEST_PRICE

                          });


            ViewData["List"] = top_10;
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Index(SearchModels form)
        {
          
            if (String.IsNullOrEmpty(form.Search))
            {
                ModelState.AddModelError("Search", "Empty input");
                ViewData["Message"] = "";


                var top10booking = (from booking in _db.BOOKING
                                    group booking by new { booking.H_NAME, booking.H_COUNTRY, booking.H_BRANCHNAME } into hotelGrps
                                    let sum = hotelGrps.Count()
                                    orderby sum descending
                                    select new
                                    {
                                        Hotel = hotelGrps.Key.H_NAME,
                                        Branch = hotelGrps.Key.H_BRANCHNAME,
                                        Country = hotelGrps.Key.H_COUNTRY

                                    });


                var top_10 = (from cheapestRoom in _db.LOWEST_HOTEL_PRICE
                              from top in top10booking
                              from hotel in _db.HOTEL

                              where top.Hotel == cheapestRoom.H_NAME &&
                                    top.Hotel == hotel.HOTEL_NAME &&
                                    top.Branch == cheapestRoom.H_BRANCHNAME &&
                                    top.Branch == hotel.BRANCHNAME &&
                                    top.Country == hotel.COUNTRY &&
                                    top.Country == cheapestRoom.H_COUNTRY

                              select new
                              {
                                  HOTELIMAGE = hotel.HOTELIMAGE,
                                  HOTEL = hotel.HOTEL_NAME,
                                  Branch = hotel.BRANCHNAME,
                                  Country = hotel.COUNTRY,
                                  Price = cheapestRoom.LOWEST_PRICE

                              });


                ViewData["List"] = top_10;
                return View();
               
            }
  
                return RedirectToAction("SearchResult", "Search", form);

        }
        public ActionResult DisplayMainPage()
        {
            return null;
        }


        public ActionResult Error() {
           return View();
        }



        
    }
}
